// components/index/entries.ts
import { ajax } from "../../utils/request";
Component({
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    listData: [
      { id: "001", uri: "/pages/car/car", title: "绑定车牌", image_path: "../../assets/keqing.jpg" },
      { id: "002", uri: "/pages/order/order", title: "订单查询", image_path: "../../assets/hutao.jpg" },
      { id: "003", uri: "/pages/vip/vip", title: "会员中心", image_path: "../../assets/keli.jpg" },
      { id: "004", uri: "/pages/coupon/coupon", title: "优惠券", image_path: "../../assets/keqing.jpg" },
      { id: "005", uri: "/pages/month/month", title: "月卡缴费", image_path: "../../assets/hutao.jpg" },
      { id: "006", uri: "/pages/invoice/invoice", title: "开具发票", image_path: "../../assets/keli.jpg" },
      { id: "007", uri: "/pages/consult/consult", title: "联系客服", image_path: "../../assets/keqing.jpg" },
      { id: "008", uri: "/pages/message/message", title: "留言反馈", image_path: "../../assets/hutao.jpg" }
    ]
  },

  /**
   * 组件的方法列表
   */
  methods: {
    handleTap(e: WechatMiniprogram.CustomEvent) {
      console.log("mine ==", e.currentTarget.dataset.uri);
      // 如果没有登录，提示弹窗，提示用户登录
      if (wx.getStorageSync("userInfo")) {
        console.log("有用户信息，则跳转至修改个人信息界面");
        wx.navigateTo({
          url: e.currentTarget.dataset.uri
        })
      } else {
        wx.showModal({
          title: "旅行者还没登录喔~",
          content: "点击确定授权登录(#^.^#)",
          success: res => {
            if (res.confirm) {
              console.log("用户点击了确定");

              wx.login({
                success: async function (res) {
                  console.log("login === ", res);
                  // 发送code，获取sessionID
                  let sessionRes = await ajax("/user/getSession", { code: res.code }, "GET")

                  // 存储token值在本地缓存中
                  wx.setStorageSync("sessionId", sessionRes.data.data);
                  console.log('sessionId == ',wx.getStorageSync("sessionId"));
                }
              })

              
              wx.getUserProfile({
                desc: '展示用户信息', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
                success: (res: any) => {
                  console.log(res);

                  let param: any = {}
                  param.encryptedData = res.encryptedData;
                  param.iv = res.iv;
                  param.nickname = res.userInfo.nickName;
                  param.photo = res.userInfo.avatarUrl;
                  param.sessionId = wx.getStorageSync("sessionId");

                  ajax("/user/authLogin", param, "POST").then(res => {
                    // 登录成功
                    console.log("res =====", res);

                    if (res.data.code === 0) {
                      // 存储token值在本地缓存中
                      wx.setStorageSync("token", res.header.token);
                    }
                    console.log('token======', wx.getStorageSync("token"));
                  })

                  // 模拟存在车牌号
                  res.userInfo.carData = [
                    { id: "001", num: "川A12345" },
                    { id: "002", num: "新A91214" },
                    { id: "003", num: "闽B66666" },
                    { id: "003", num: "闽B66666" },
                    { id: "003", num: "闽B66666" },
                  ]

                  // 将用户信息存储到本地缓存
                  let userInfo: any = res.userInfo
                  wx.setStorageSync("userInfo", userInfo)

                  // // 将用户信息传给父组件
                  // this.triggerEvent("getInfo",userInfo)

                  // 跳转页面
                  wx.navigateTo({
                    url: e.currentTarget.dataset.uri
                  })

                }
              });

            } else {
              console.log("用户点击了取消");
              return;
            }
          }
        })
      }
    }
  }
})
